<?php

use Biz\model\ShopCategoryClassify;
use Biz\model\wiki\BrandAreaModule;
use Biz\service\BrandPreFecture;
use Biz\service\wiki\Wiki;
use Biz\model\brand\Brand;
use Doraemon\model\Member as dbUser;
use Doraemon\service\statSign\Basic as statSign;
use Doraemon\service\User as sUser;
use Doraemon\service\WikiBasic\Schedule;
use Doraemon\tools\ActionLog;
use Doraemon\tools\BlockType;
use Doraemon\tools\LinkFormat;
use Doraemon\cache\UserCounter;

/**
 * @deprecated
 */
class BrandListAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_NOTHING;

    const HOT_BRAND = '热门品牌';
    const DEFAULT_PAGESIZE = 20;

    public function main()
    {
        $data = [];
        $data['is_over'] = 0;

        $page = $this->getRequest()->get('page', 1);
        $keyword = $this->getRequest()->get('keyword', '');
        if ($page == 1 && $keyword == self::HOT_BRAND) {
            $hotBrands = BrandAreaModule::getAllByWhere(['brand_area_id' => BrandAreaModule::HOT_BRAND_AREA, 'module_status' => BrandAreaModule::STATUS_NORMAL, 'ORDER' => ['sort' => 'ASC']]);
            $recommends = [];
            if ($hotBrands && is_array($hotBrands)) {
                $index = 1;
                foreach ($hotBrands as $hot_info) {
                    $link = (string)LinkFormat::generateLink($hot_info['link_type'], $hot_info['link_params'])['app_template'];
                    $recommends[] = [
                        'id' => $hot_info['id'],
                        'title' => $hot_info['title'],
                        'icon' => $hot_info['pic_url'],
                        'link' => $link,
                        "statSign" => statSign::genStatSign(statSign::MODULE_BRAND_ZONE_RECOMM_BANNER, [
                            statSign::EXTEND_KEY_CONFIG_ID => $hot_info['id'], statSign::EXTEND_KEY_INDEX => $index], ['module_name' => $hot_info['title'], 'brand_id' => (string)explode('hhz://brand_prefecture:', $link)[1]])
                    ];
                    $index++;
                }
            }
            $data['hot_brands'] = array_values($recommends);
        }
        $tab_name = '热门品牌';
        if ($keyword == self::HOT_BRAND) {
            //算法推荐品牌列表
            $recommend_brands = (array)BrandPreFecture::getRecommendBrands($this->uid, 0, 0, self::DEFAULT_PAGESIZE);
        } else {
            $front_id = (int)ShopCategoryClassify::getInfoByName($keyword)['id'];
            $tab_name = (string)ShopCategoryClassify::getInfo($front_id)['classify_name'];
            if ($front_id) {
                $redis = new \Hhz\Redis\newCache(\hhz\constant\redis\redisKeys::KEY_B_CACHE_CATEGORY_CLASSIFY_);
                $cate_ids = $redis->getCache($front_id);
                $recommend_brands = (array)BrandPreFecture::getRecommendBrands($this->uid, $front_id, $cate_ids, self::DEFAULT_PAGESIZE);
            }

        }

        $brand_ids = array_column((array)$recommend_brands['list'], 'obj_id');

        $total = (int)$recommend_brands['total_num'];
        if ($total <= ($page * self::DEFAULT_PAGESIZE)) {
            $data['is_over'] = 1;
            if ($page != 1) {
                $curr_num = (int)($total - ($page - 1) * self::DEFAULT_PAGESIZE);
                $brand_ids = array_slice($brand_ids, 0, $curr_num);
            }
        }

        if ($page == 1) {
            $index = rand(1, count($brand_ids) - 1);
            $arr1 = array_slice($brand_ids, 0, $index);
            $arr1[] = 2639;
            $arr2 = array_slice($brand_ids, $index);
            $brand_ids = array_merge($arr1, $arr2);
            $brand_ids = array_unique($brand_ids);
        }
        $uids = Brand::getUidsByIds($brand_ids);
        $user_infos = sUser::getBrandCards($uids);
        $wikis = Schedule::getGroupBrandGoods($brand_ids);
        $sort = array_flip($brand_ids);
        $brand_list = [];
        if ($uids && is_array($uids)) {
            foreach ($uids as $brand_id => $uid) {
                $wikiIds = ($wikis[$brand_id] && isset($wikis[$brand_id])) ? array_column($wikis[$brand_id], 'id') : [];
                $wiki_list = Wiki::getWikiList($wikiIds);
                $wiki_list = $wiki_list ? array_values($wiki_list) : [];
                if ($wiki_list && is_array($wiki_list)) {
                    $brand_list[$sort[$brand_id]] = [
                        "type" => BlockType::TYPE_BRAND_LIST,
                        "brand" => [
                            "user_info" => $user_infos[$uid],
                            "counter" => UserCounter::getOne($uid),
                            "wiki_list" => $wiki_list
                        ],
                        "statSign" => statSign::genStatSign(statSign::MODULE_BRAND_ZONE_CATEGORY_BRAND_LIST, [
                            statSign::EXTEND_KEY_OBJ_ID => $brand_id, statSign::EXTEND_KEY_INDEX => $sort[$brand_id], 'page' => statSign::EXTEND_KEY_PAGE], ['module_name' => '品牌专区分类推荐品牌列表', 'tab_name' => $tab_name])
                    ];
                }
                if ($brand_id == 2639) {//F+ 品牌没有商品，展示固定的图片和标题
                    $brand_list[$sort[$brand_id]] = [
                        "type" => BlockType::TYPE_BRAND_LIST,
                        "brand" => [
                            "user_info" => $user_infos[$uid],
                            "counter" => UserCounter::getOne($uid),
                            "wiki_list" => BrandPreFecture::getTempWikiList($brand_id)
                        ],
                        "statSign" => statSign::genStatSign(statSign::MODULE_BRAND_ZONE_CATEGORY_BRAND_LIST, [
                            statSign::EXTEND_KEY_OBJ_ID => $brand_id, statSign::EXTEND_KEY_INDEX => $sort[$brand_id], 'page' => statSign::EXTEND_KEY_PAGE], ['module_name' => '品牌专区分类推荐品牌列表', 'tab_name' => $tab_name])
                    ];
                }
            }
        }

        rsort($brand_list);
        $data['list'] = array_values($brand_list);


        $data['tips'] = [
            "title" => '如何入驻？',
            "link" => ""
        ];

        ActionLog::log($this->uid, ['page' => $page, 'tab_name' => $keyword], ActionLog::BRAND_ZONE_PAGE);

        $this->setSuccess($data);
    }

}